/*
 */
package com.ericsson.excelbeanimporter.impl;

import com.ericsson.excelbeanimporter.BeanReader;
import com.ericsson.excelbeanimporter.ExcelBean;
import com.ericsson.excelbeanimporter.ExcelBeanImportException;
import com.ericsson.excelbeanimporter.ExcelBeanImporter;
import com.ericsson.excelbeanimporter.ImportResult;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

/**
 *
 * @author eluibon
 */
public class SimpleExcelBeanImporter<T extends ExcelBean> implements ExcelBeanImporter {
    
    private Logger logger = Logger.getLogger(this.getClass().getName());
    
    BeanReader reader ;
    
    public SimpleExcelBeanImporter(BeanReader reader) {
        
        this.reader = reader ;
    }

    public <T extends ExcelBean> ImportResult<T> importFromSheet(Sheet sheet, int startRow, int endRow, Class<T> type) throws ExcelBeanImportException {
     
        SimpleImportResult<T> result = new SimpleImportResult<T>() ;
        
        // TODO : esiste un modo di verificare che il bean T non abbia tipi primitivi ? in questo modo si risolve il problema di celle blank (nulle)
        //        che potrebbe essere un problema matchare su valori primitivi, a meno che non si sollevi un errore in questo caso
        
        logger.debug("Processing sheet " + sheet.getSheetName() + " from row " + startRow + " to row " + endRow);
         
        for ( int i = startRow ; i <= endRow ; i++ ) {
            
            Row row = sheet.getRow(i) ;
            
            logger.debug("processing row " + (i+1) ) ; // POI rows are zero-bases, Excel are 1-based
            
            T obj = reader.readFromRow(row,type) ;

            result.addBean(obj);            
        }

        return result ;
    }   
    
}
